// pages/record_export/export.js
const util = require("../../utils/util.js")
const app = getApp()
const {
  $Message
} = require('../../dist/base/index');
const {
  $Toast
} = require('../../dist/base/index');

Page({
  data: {
    spinShow: false,
    fileUrl: '',
    items: [],
    startDate: util.formatTime(new Date(), 1),
    startTime: util.formatTime(new Date(), 2),
    endDate: util.formatTime(new Date(), 1),
    endTime: util.formatTime(new Date(), 2),
  },
  onLoad: function(options) {
    this.getAddress()
  },
  onShow: function() {
    if (app.globalData.flushExport) {
      app.globalData.flushExport = false
      this.getAddress()
    }
  },
  getAddress() {
    let that = this
    let groupId = null
    if (app.globalData.group) {
      groupId = app.globalData.group._id
    }
    wx.cloud.callFunction({
      name: 'getAddress',
      data: {
        groupId: groupId
      }
    }).then(res => {
      that.setData({
        items: res.result
      })
    })
  },
  formSubmit: function(e) {
    var that = this
    let item = e.detail.value
    if (item.choosedIds.length == 0) {
      $Message({
        content: '请选择出入口',
        type: 'warning',
        duration: 1
      });
      return
    }
    if (!util.judgeTime(item.startDate, item.startTime, item.endDate, item.endTime)) {
      $Message({
        content: '时间段不对',
        type: 'warning',
        duration: 1
      });
      return
    }
    this.setData({
      spinShow: true
    })
    wx.cloud.callFunction({
      name: 'queryRegistration',
      data: {
        addressIds: item.choosedIds,
        startDate: item.startDate,
        startTime: item.startTime,
        endDate: item.endDate,
        endTime: item.endTime
      }
    }).then(res => {
      if (res.result.list.length == 0) {
        this.setData({
          spinShow: false
        })
        $Toast({
          content: '记录为空',
          image: "../../images/icon/sad.png"
        });
      } else {
        that.savaExcel(res.result.list)
      }
    })
  },
  //把数据保存到excel里，并把excel保存到云存储
  savaExcel(datas) {
    let that = this
    wx.cloud.callFunction({
      name: "excel",
      data: {
        datas: datas
      },
      success(res) {
        that.getFileUrl(res.result.fileID)
        that.setData({
          spinShow: false
        })
        $Toast({
          content: '导出成功',
          image: "../../images/icon/happy.png"
        });
      },
      fail(res) {
        console.log("保存失败", res)
      }
    })
  },
  bindStartDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  bindStartTimeChange: function(e) {
    this.setData({
      startTime: e.detail.value
    })
  },
  bindEndDateChange: function(e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  bindEndTimeChange: function(e) {
    this.setData({
      endTime: e.detail.value
    })
  },
  //获取云存储文件下载地址，这个地址有效期一天
  getFileUrl(fileID) {
    let that = this;
    wx.cloud.getTempFileURL({
      fileList: [fileID],
      success: res => {
        // get temp file URL
        that.setData({
          fileUrl: res.fileList[0].tempFileURL
        })
      },
      fail: err => {}
    })
  },
  //复制excel文件下载链接
  copyFileUrl() {
    let that = this
    wx.setClipboardData({
      data: that.data.fileUrl,
      success(res) {
        wx.getClipboardData({
          success(res) {}
        })
      }
    })
  },
  onHide: function() {
    this.setData({
      fileUrl: ''
    })
  },
})